<template>
  <view class="deposit-page">
    <!-- 金额展示 -->
    <view class="amount-box">
      <text class="amount">￥{{yajin}}</text>
    </view>

    <!-- 温馨提示 -->
    <view class="tips">
      <text class="tips-title">温馨提示</text>
      <text class="tips-text">还车后，押金将自动原路退回至您的账户。</text>
    </view>

    <!-- 支付方式 -->
    <view class="pay-method">
      <image class="wechat-icon" src="https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/weier/weichat_pay.png"></image>
      <text class="wechat-text">微信支付</text>
      <text class="check-icon">✔</text>
    </view>

    <!-- 支付按钮 -->
    <view class="pay-btn" @tap="add_order()">立即支付</view>
  </view>
</template>

<script>
import common_config from '../../static/js/common.js';
var config = common_config.config;
var that;	
var userInfo;
export default {
	data() {
		return {
			yajin:0,
			rowid:'',
			order_rowid:'',
			pay_order:''
		}
	},
	name: "DepositPage",
	onLoad(options) {
		that = this;
		that.rowid = options.rowid;
		that.yajin = options.money;
		that.plusReady();
	},
	methods: {
		//初始化
		plusReady(){
		  	userInfo = uni.getStorageSync('userInfo');
		  	if(userInfo){
		  		userInfo = JSON.parse(userInfo);
		  		that.if_login = true;
		  		//that.load_datas();
		  	}else{
						uni.switchTab({
							url:'/pages/index/me'
						});
		  		that.if_login = false;
		  	}
		  	
		},	
		//创建订单
		add_order(){
			uni.request({
			  url: config.request_url+'/workflow/hooks/NjhkNWZkZGUxZjJmYmYzOTQ0YjhiNDA0',
			  method: 'POST',
			  header: { 'Content-Type': 'application/json' },
			  data: {
			    "appKey": config.appKey,
			    "sign": config.sign,
			    "UserId":userInfo.rowid,// 用户rowid
				"MachineId":that.rowid// 设备rowid
			  },
			  success: (res) => {
			    res = res.data;
				res = res.data;
			    if(res.success){
					//0afc51cd-e50e-4bc1-a4cf-d591838ab3f2
					//common_config.calert(res.data.dzyjRowid);
					that.order_rowid = res.data.dzyjRowid;
					that.load_datas();
			    }else{
			      uni.showToast({ title: res.data?.msg || '数据获取失败', icon: 'none' });
			    }
			  },
			  fail: (err) => {
			    console.error('请求失败:', err);
			    uni.showToast({ title: '网络错误', icon: 'none' });
			  }
			})  
		},
		load_datas(){
			//console.log(121212);
			uni.request({
			  url: config.request_url+'/v2/open/worksheet/getFilterRows',
			  method: 'POST',
			  header: {
				'Content-Type': 'application/json'
			  },
			  data: {
				"appKey": config.appKey,
				"sign": config.sign,
				"worksheetId": "zfjl",
				"pageSize": 50,
				"pageIndex": 1,
				"filters": [
					{
						"controlId": "rowid",
						"dataType": 2,
						"spliceType": 1,
						"filterType": 2,
						"value": that.order_rowid// 支付记录rowid ，多条记录用英文逗号隔开
					}
				]
			  },
			  success: (res) => {
				//console.log('返回结果:', res.data);
				res = res.data;
				//console.log(res);
				if(res.success){
					//that.data_info = res.data;
					that.pay_order = res.data.rows[0].zfjlbh;
					//common_config.calert(that.pay_order);
					that.sub_order();
				}else{
					 uni.showToast({ title: res.data.msg || '数据获取失败', icon: 'none' });
				}
			  },
			  fail: (err) => {
				console.error('请求失败:', err);
				uni.showToast({ title: '网络错误', icon: 'none' });
			  }
			})  
		},
		//提交支付
		sub_order(){
			uni.showLoading({
				title:'正在提交支付...'
			})
		  uni.request({
		    url: config.weixin_url+'/api/Login/GetWxPrepayId',
		    method: 'POST',
		    header: { 'Content-Type': 'application/json' },
		    data: {
		      openId:userInfo.openid,
			  totalMoney:that.yajin*100,
			  //totalMoney:0.01*100,
			  billNo:that.pay_order,
			  title:'押金',
			  sign:'123455'
		    },
		    success: (res) => {
				uni.hideLoading();
		      res = res.data;
		     if(res.status == 200){
				 var datas = res.response;
				 uni.requestPayment({
				 	provider: 'wxpay',
				 	orderInfo: that.pay_order,
				 	timeStamp:datas.timeStamp,
				 	nonceStr:datas.nonceStr,
				 	package:datas.package,
				 	signType:datas.signType,
				 	paySign:datas.paySign,
				 	success: function () {
						userInfo.sfsx = '是';
						uni.setStorageSync('userInfo',JSON.stringify(userInfo));
				 		uni.redirectTo({ url:'pay_suc?act=duanzu'});
				 	},
				 	fail: function (err) {
				 		console.log(err);
				 		common_config.calert('支付失败');
				 	}
				 });
			 }else{
				 common_config.calert(res.msg);
			 }
		    },
		    fail: (err) => {
		      console.error('请求失败:', err);
		      uni.showToast({ title: '网络错误', icon: 'none' });
		    }
		  })  
		},
		toPay() {
		  uni.showToast({
			title: "支付功能待接入",
			icon: "none"
		  });
		}
  }
};
</script>

<style>
.deposit-page{background:#fff;height:100vh;font-family:Arial;}
.nav-bar{display:flex;align-items:center;justify-content:space-between;padding:10px 15px;font-size:16px;position:relative;}
.back-icon{font-size:20px;}
.nav-title{position:absolute;left:50%;transform:translateX(-50%);font-weight:bold;}
.nav-right{display:flex;gap:10px;}
.amount-box{margin:20px auto;width:90%;background:linear-gradient(to right,#5dd5ff,#00aaff);border-radius:12px;padding:30px 0;text-align:center;}
.amount{color:#000;font-size:28px;font-weight:bold;}
.tips{margin:10px 20px;color:#666;font-size:14px;line-height:22px;}
.tips-title{font-weight:bold;margin-right:6px;}
.pay-method{display:flex;align-items:center;justify-content:space-between;padding:15px 20px;border-bottom:1px solid #f0f0f0;}
.wechat-icon{width:22px;height:22px;margin-right:10px;}
.wechat-text{flex:1;color:#333;font-size:16px;margin-left:8px;}
.check-icon{color:#06c25f;font-size:18px;}
.pay-btn{margin:40px 20px;background:#222;color:#fff;text-align:center;font-size:18px;padding:14px 0;border-radius:50px;}
</style>
